package cn.tedu.controller;

import cn.tedu.entity.User;
import cn.tedu.mapper.HttpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HttpController {
    @Autowired
    private HttpMapper mapper;

    //value属性表示访问路径,method表示请求方式
    @RequestMapping(value = "getUser",method = RequestMethod.GET)
    public String getUserAll() {
        List<User> list = mapper.getUserAll();
        return list.toString();
    }

    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    public User getUser(int id) {
        User user = mapper.getUser(id);
        return user;
    }

    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    public String postUser(@RequestBody User user) {
        mapper.postUser(user);
        return "添加成功!";
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public String putUser(@RequestBody User user) {
        mapper.putUser(user);
        return "修改成功!";
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    public String deleteUser(int id){
        mapper.deleteUser(id);
        return "删除成功";
    }
}
